local FishTopView = MClass("FishTopView")

local ColorWarnning = cc.c3b(242, 0, 0)
local ColorNormal = cc.c3b(242, 253, 135)

function FishTopView:ctor()
    --self.root 默认为根节点
    --设置事件
    self.escButton = self.root:findChild("btn_esc")
    assert(self.escButton)
    self.setButton = self.root:findChild("btn_set")
    assert(self.setButton)
    self.autoButton = self.root:findChild("btn_auto")
    assert(self.autoButton)
    self.mapButton = self.root:findChild("btn_map")
    assert(self.mapButton)
    self.roundLabel = self.root:findChild("text_tlt")
    self.roundLabel:setVisible(false)
    self.dotBg = self.root:findChild("bg/dot")
    self.dotBg:setVisible(false)

    --time label
    self.timeLabel = self.root:findChild("clock/time")
    assert(self.timeLabel)

    self.gradeTimeLabel = self.root:findChild("grade/time")
    self.gradeIcon = self.root:findChild("grade/icon")

    local config = GD:queryEventModeById(Enums.TeamTarget.fishVSZombie)
    local strCondition = config.condition  -- 1500;900;600
    local conditionTime = {}
    for w in string.gmatch(strCondition, "%d+") do
        table.insert(conditionTime, tonumber(w))
    end
    self.conditionTime = conditionTime

    self.bShowEsc = false
    self.escButton:setVisible(false)

    self:registerEvents()
end

function FishTopView:registerEvents()
    BattleUIEvent:addObjToFrameUpdate(self,FishTopView.updateFrame)
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_FISH_INIT_TOP,handler(self,FishTopView.init))
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_TIME,handler(self,FishTopView.setTime))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.ESCAPE_SHOWUI,handler(self,FishTopView.showEscape))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_SET_ABLE,handler(self,FishTopView.enableEsc))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_CANCEL_AUTO,handler(self,FishTopView.cancelAutoBattle))

    --逃跑
    local function touchEvent_Esc(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            local function okFunc()
                BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_ESCAPE)
            end
            app:popMessage("escape", nil, {ok = okFunc}, Enums.ToastTag.battle)
        end
    end
    self.escButton:addTouchEventListener(touchEvent_Esc)

    --设置
    local function touchEvent_setting(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            UIHandler:handleMessage({id=Constant.FUNC_TYPE_SET, mode="open", data=2})
        end
    end
    self.setButton:addTouchEventListener(touchEvent_setting)

     --小地图
    local function touchEvent_map(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
                BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SHOW_MELEE_MAP)
        end
    end
    self.mapButton:addTouchEventListener(touchEvent_map)

    local setting = UD.setting
    self.autoButton:setBright(not setting:getIsAutoBattle())
    --自动战斗
    local function touchEvent_autoBattle(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            
            if setting:getIsAutoBattle() then
                sender:setBright(true)
                setting:setAutoBattle(false)
                EM:notify("onSettinAutoBattle", false)
                self.lastAutoBattle = false
            else
                sender:setBright(false)
                setting:setAutoBattle(true)
                EM:notify("onSettinAutoBattle", true)
                self.lastAutoBattle = true
            end
        end
        UD:updateSetting(setting)
    end
    self.autoButton:addTouchEventListener(touchEvent_autoBattle)
    self.lastAutoBattle = setting:getIsAutoBattle()
end

function FishTopView:cancelAutoBattle()
    --取消自动战斗
    EM:notify("onSettinAutoBattle", false)
    self.startTime = app.session:getServerTime() or app:getUTCTime()
end

function FishTopView:updateFrame()
    if not self.startTime then
        return
    end
    self.duleTime = app.session:getServerTime() or app:getUTCTime()
    if self.duleTime-self.startTime > Constant.BattleAutoCancelInterval then
        EM:notify("onSettinAutoBattle", self.lastAutoBattle)
        self.startTime = nil
    end
end

function FishTopView:init(event)
    self.battleLength = (event.battleLength or 0) / 1000
    self.conditionTime[Enums.activityRewardGrade.bronze] = self.battleLength - self.conditionTime[Enums.activityRewardGrade.bronze]
    self.conditionTime[Enums.activityRewardGrade.silver] = self.battleLength - self.conditionTime[Enums.activityRewardGrade.silver]
    self.conditionTime[Enums.activityRewardGrade.gold] = self.battleLength - self.conditionTime[Enums.activityRewardGrade.gold]

    self.grade = Enums.activityRewardGrade.gold
    local second = self.conditionTime[Enums.activityRewardGrade.gold] -- 总秒
    local min = math.floor(second/60) -- 分
    second = math.floor(second%60) -- 秒
    self.gradeTimeLabel:setString(string.format("%02d:%02d",min,second))
end

function FishTopView:setTime(time)
    time = tonumber(time)
    local second = math.floor(time/1000) -- 总秒

    if second < self.conditionTime[Enums.activityRewardGrade.gold]
      and self.grade == Enums.activityRewardGrade.gold then
        self.grade = Enums.activityRewardGrade.silver
        local secondHint = self.conditionTime[Enums.activityRewardGrade.silver] -- 总秒
        local minHint = math.floor(secondHint/60) -- 分
        secondHint = math.floor(secondHint%60) -- 秒
        self.gradeTimeLabel:setString(string.format("%02d:%02d",minHint,secondHint))
        self.gradeIcon:loadTexture("ui_resources/battle/fish_icon_2.png", 1)
    elseif second < self.conditionTime[Enums.activityRewardGrade.silver]
      and self.grade == Enums.activityRewardGrade.silver then
        self.grade = Enums.activityRewardGrade.bronze
        local secondHint = self.conditionTime[Enums.activityRewardGrade.bronze] -- 总秒
        local minHint = math.floor(secondHint/60) -- 分
        secondHint = math.floor(secondHint%60) -- 秒
        self.gradeTimeLabel:setString(string.format("%02d:%02d",minHint,secondHint))
        self.gradeIcon:loadTexture("ui_resources/battle/fish_icon_3.png", 1)
    elseif second < self.conditionTime[Enums.activityRewardGrade.bronze]
      and self.grade == Enums.activityRewardGrade.bronze then
        self.grade = Enums.activityRewardGrade.none
        self.gradeTimeLabel:setVisible(false)
        self.gradeIcon:setVisible(false)
    end

    local min = math.floor(second/60) -- 分
    second = math.floor(second%60) -- 分
    self.timeLabel:setString(string.format("%02d:%02d",min,second))
    if time < 60000 then
        self.timeLabel:setColor(ColorWarnning)
    else
        self.timeLabel:setColor(ColorNormal)
    end
end

function FishTopView:showEscape()
    self.bShowEsc = true
    self.escButton:setVisible(true)
    self.escButton:setEnabled(false)
    self.escButton:setOpacity(155)
end

function FishTopView:enableEsc(bAble)
    if bAble and self.bShowEsc then
        self.escButton:setVisible(true)
        self.escButton:setEnabled(true)
        self.escButton:setOpacity(255)
    end
end

function FishTopView:showRound(round)
    self.roundLabel:setVisible(true)
    self.dotBg:setVisible(true)
    local roundNumsStr = string.format(L("zombieRoundText"), round)
    self.roundLabel:setString(roundNumsStr)
end


return FishTopView